package com.example.month11.fragment2;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.fragment.app.FragmentTransaction;

import com.example.month11.fragment2.base.BaseActivity;
import com.example.month11.fragment2.base.BaseFragment;
import com.example.month11.fragment2.fragments.MainMenuFragment;
import com.example.month11.fragment2.fragments.Page01Fragment;
import com.example.month11.fragment2.fragments.Page02Fragment;
import com.example.recyclerview01.R;

public class FragmentShowActivity extends BaseActivity implements
        MainMenuFragment.Callback, Page01Fragment.Callback,
        Page02Fragment.Callback {
    private static final String TAG = "FragmentShowActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_show);

        initViews();
    }

    @Override
    protected void initViews() {
        MainMenuFragment homeMenu = MainMenuFragment.newInstance(); // 默认显示注册第一个页面
        getSupportFragmentTransaction()
                .replace(R.id.container, homeMenu, MainMenuFragment.TAG)
                .commit();
    }

    public void clickFAB(View view) {
    }

    @Override
    public void showPage01(BaseFragment fragment) {
        Log.d(TAG, "showPage01");
        FragmentTransaction ft = getSupportFragmentTransaction();
        ft.hide(fragment);
        Page01Fragment lpFragment = findFragment(Page01Fragment.TAG);
        if (lpFragment == null) {
            lpFragment = Page01Fragment.newInstance();
            ft.add(R.id.container, lpFragment, Page01Fragment.TAG).addToBackStack(Page01Fragment.TAG);
        } else {
            if (fragment.isHidden()) {
                ft.show(lpFragment);
            }
            lpFragment.onFragmentResume();
        }
        ft.commit();
    }

    @Override
    public void showPage02(BaseFragment fragment) {
        Log.d(TAG, "Fragment01 调用：showPage02");
        FragmentTransaction ft = getSupportFragmentTransaction();
        ft.hide(fragment);
        Page02Fragment lpFragment = findFragment(Page02Fragment.TAG);
        if (lpFragment == null) {
            lpFragment = Page02Fragment.newInstance();
            ft.add(R.id.container, lpFragment, Page02Fragment.TAG).addToBackStack(Page02Fragment.TAG);
        } else {
            if (fragment.isHidden()) {
                ft.show(lpFragment);
            }
            lpFragment.onFragmentResume();
        }
        ft.commit();
    }

    @Override
    public void showMessage(String msg) {
        // 子Fragment传值给Activity。
        Log.d(TAG, "FragmentShowActivity showMessage:" + msg);
    }
}